package com.xxhui.guidepagedemo.ViewPage;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2016/8/8.
 */
public class ScaleStackViewPage extends ViewPager {

    private float align_to_left_pixels;
    private float XYScale; //X轴和Y轴的缩放比例

    private static final float SCALE_MAX = 0.5f;   //最大的缩小比例

    private HashMap<Integer, View> pageMap = new LinkedHashMap<Integer, View>();//一个有序的map

    private View leftPage;

    private View rightPage;

    public ScaleStackViewPage(Context context) {
        super(context);
    }

    public ScaleStackViewPage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     *
     * @param position viewpage显示第几页
     *@param offset 值为0~1，表示页面位置的偏移
     * @param offsetPixels 在屏幕宽度的一个值，横屏为０~720（我手机）
     */
    @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {

        leftPage = pageMap.get(position);

        if(leftPage!=null){
            leftPage.bringToFront();
        }
        rightPage = pageMap.get(position+1);

        if(rightPage!=null){
            XYScale = (1 - SCALE_MAX) * offset + SCALE_MAX;
            rightPage.setScaleX(XYScale);
            rightPage.setScaleY(XYScale);
            align_to_left_pixels = -getWidth() - getPageMargin() +offsetPixels;
            rightPage.setTranslationX(align_to_left_pixels);
        }
        super.onPageScrolled(position, offset, offsetPixels);
    }

    public void updatePageMap(Integer position, View v) {
        this.pageMap.put(position,v);
    }
}
